import load from '@/utils/jsapi.js';
import JSON from 'json3';
 import maplayersUtils from '@/utils/maputils/maplayersUtils.js';


const maplayerrenderoperate = {
  async maplayerrender(Parameters) {
    if (Parameters.layername == undefined || Parameters.layername == null)
      return


    window.renderlayers=[];
    const mapoperationlayers = window.map.layers;
    maplayersUtils.filterlayers(mapoperationlayers)

    // const rendererlayer = window.map.findLayerById(Parameters.layername);
    if (window.renderlayers == undefined || window.renderlayers == null || !window.renderlayers.length) return;

    if (Parameters.isrenderer == false) {
      if (Parameters.defaultrender != undefined && Parameters.defaultrender != null)
        window.renderlayers.map(function (item, ary, index) {
          if (item.id.indexOf(Parameters.layername)>-1)
           item.renderer = Parameters.defaultrender;
           if (Parameters.labelsVisible == false) {
             item.labelsVisible = false
           } else {
             item.labelsVisible = true
           }
        })
      return
    }

    if (Parameters.rendercolor == undefined || Parameters.rendercolor == null)
      return

    if (Parameters.rendererfield == undefined || Parameters.rendererfield == null)
      return


    let defSymbol = null;
    if (Parameters.defaultSymbol == undefined || Parameters.defaultSymbol == null) {
      defSymbol = {
        type: "simple-fill",
        color: [255, 255, 255, 1],
        outline: {
          color: [255, 255, 0, 1],
          width: "5px"
        }
      }
    } else {
      defSymbol = Parameters.defaultSymbol;
    }

    let layerrenderer = {
      type: "unique-value",
      field: Parameters.rendererfield,
      defaultLabel: "",
      defaultSymbol: defSymbol,
      uniqueValueInfos: []
    };

    Parameters.rendercolor.map(function (item, key, ary) {
      let uniquesymbol = {
        label: item.label,
        symbol: {
          type: "simple-fill",
          color: item.fillcolor,
          outline: {
            color: item.outlinecolor,
            width: item.outlinewith
          }
        },
        value: item.code
      };
      layerrenderer.uniqueValueInfos.push(uniquesymbol);
    });
    renderlayers.map(function (item, ary, index) {
     if (item.id.indexOf(Parameters.layername) > -1)
       item.renderer = layerrenderer;
        if (Parameters.labelsVisible == false) {
             item.labelsVisible = false
           } else {
             item.labelsVisible = true
          }
    })
  }
}

export default maplayerrenderoperate
